package com.gongpingjialoan.activity.car;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.bean.RecordBean;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.StepMonitor;
import com.gongpingjialoan.widget.LoadingDialog;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.smarttablayout.SmartTabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Administrator on 2016/6/21.
 */
public class PushSettingActivity extends BaseActivity implements View.OnClickListener{

    private NetDataJson mNetDataJson;

    public LoadingDialog loadingDialog;

    private SmartTabLayout mSmartTabLayout;

    public ViewPager mViewPager;

    private RemindPirceFragment mRemindPirceFragment;

    private VipCarFragment mVipCarFragment;

    private FindCarFragment mFindCarFragment;

    private int currentItem = 0 ;

    private String[] actions = {"my_subscribeRecord_remind", "my_subscribeRecord_vipBuy", "my_subscribeRecord_findCar"};

    private String[] titles = {"调价提醒", "Vip购车订阅", "帮你找车"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.push_setting_layout);
        loadingDialog = new LoadingDialog(this);
        EventBus.getDefault().register(this);
        setTitle("订阅记录");
        initView();
        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mNetDataJson!=null){
            mNetDataJson.cancelTask();
            mNetDataJson = null;
            EventBus.getDefault().unregister(this);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        StepMonitor.getInstance().addMonitor(new Monitor("enter", actions[currentItem], System.currentTimeMillis(), 1f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(Object o) {
        if ("register_vip".equals(o.toString())) {
            getData();
        }
    }


    private  void getData(){
        if(mNetDataJson == null) {
            mNetDataJson = new NetDataJson(new NetDataJson.OnStringListener() {
                @Override
                public void onDataJsonError(String errorMessage) {
                    loadingDialog.dismiss();
                    Toast.makeText(PushSettingActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onDataJsonUpdate(String json) {
                    loadingDialog.dismiss();
                    Gson gson = new Gson();
                    RecordBean recordBean = gson.fromJson(json , RecordBean.class);
                    if("success".equals(recordBean.getStatus())){
                        mRemindPirceFragment.setData(recordBean);
                        mVipCarFragment.setData(recordBean);
                        mFindCarFragment.setData(recordBean);
                    }else {
                        Toast.makeText(PushSettingActivity.this, recordBean.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                }
            }, 1);
        }
        mNetDataJson.setUrl(API.get_records);
        mNetDataJson.request("get");
        loadingDialog.show();
    }



    private void  initView(){
        mSmartTabLayout = (SmartTabLayout) findViewById(R.id.tableLayout);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mRemindPirceFragment = new RemindPirceFragment();
        mVipCarFragment = new VipCarFragment();
        mFindCarFragment = new FindCarFragment();
        mViewPager.setAdapter(new DetailPager(getSupportFragmentManager()));
        mViewPager.setOffscreenPageLimit(3);
        mSmartTabLayout.setViewPager(mViewPager);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentItem = position;
                StepMonitor.getInstance().addMonitor(new Monitor("enter", actions[currentItem], System.currentTimeMillis(), 1f));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {

    }


    private class DetailPager extends FragmentPagerAdapter {

        public DetailPager(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return mRemindPirceFragment;
            } else if (position == 1) {
                return mVipCarFragment;
            } else if (position == 2) {
                return mFindCarFragment;
            }
            return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

}
